﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateThread : System.Web.UI.Page
{
    string m_fid, m_uid,m_content ,m_title;


    protected void Page_Load(object sender, EventArgs e)
    {
        //判断是否回帖速度过快
        if (Session["LastPostTime"] != null)
        {
            DateTime dt = (DateTime)(Session["LastPostTime"]);
            if (MemDisplay.IsWaterPost(dt))
                CGeneral.GoToErrorPage(Response, "custom", "您说话太快了");
        }

        CGeneral.CheckLogin(Request, Session);
        if (null == Request.QueryString["fid"])
            CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "fid不正确");
        if (null == Session["uid"])
            CGeneral.GoToErrorPage(Response, "NeedLogin");
        int fid = Check.CheckToInt(Request.QueryString["fid"]);
        if(fid==-1)
            CGeneral.GoToErrorPage(Response, "custom","Fid错误");
        else if(!CForum.isFidExist(fid))
            CGeneral.GoToErrorPage(Response, "custom", "不存在该板块");

        
        //权限管理未设置
        Permission per = new Permission(int.Parse(Session["gid"].ToString()));
        if(per.awpost==0)
            CGeneral.GoToErrorPage(Response, "custom", "YOU NEED MORE POWER");



        //是否为AOJ题目页面
        createthread_problem_wrap.Visible = false;

        bool isAOJProbPage = false;
        if (Request.QueryString["fid"] == System.Configuration.ConfigurationManager.AppSettings["ProbForumId"])
        {
            createthread_problem_wrap.Visible = true;
            isAOJProbPage = true;
        }
        if (IsPostBack)
        {
            
            m_fid = Request.QueryString["fid"];
            m_title = createthread_title.Text;
            m_content = createthread_content.InnerText;
            //此处处理插入帖子到数据库
            Topic t = new Topic();
            t.fid = int.Parse(m_fid);
            t.uid = int.Parse(Session["uid"].ToString());
            t.title = m_title;
            t.text = m_content;
            t.posttime = DateTime.Now;
            t.postip = Request.ServerVariables["REMOTE_ADDR"].ToString();
            t.edittime = null;
            t.edituid = null;
            t.lastreptime = t.posttime;
            t.ishighlight = 0;
            t.istop = 0;
            t.isjinghua = 0;
            t.viewnum = 0;
            t.replynum = 0;
            t.status = 1;
            if (isAOJProbPage == true)
            {
                int probID = Check.CheckToInt(createthread_problemid.Text);
                if (probID == -1)
                    CGeneral.GoToErrorPage(Response, "custom", "发帖失败", "AOJ题号错误");
                t.ProbID = probID;
            }
            else
                t.ProbID = -1;

            
            if (!Check.CheckTopicTitle(t.title))
                CGeneral.GoToErrorPage(Response, "custom", "发帖失败", "帖子标题长度错误");
            if (!Check.CheckPostLegal(t.text))
                CGeneral.GoToErrorPage(Response, "custom", "发帖失败", "帖子内容长度错误[2,4000]");
            t.Publish();
            Session["LastPostTime"] = DateTime.Now;
            //跳转到 帖子页面
            string str = "";
            str += "ViewTopic.aspx?tid=" + t.tid.ToString() + "&page=1";
            SaveData.IncMemTopNum(int.Parse(Session["uid"].ToString()), DateTime.Now);//更新个人发帖信息
            Response.Redirect(str);
        } 
        
    }
}